package com.ada.jms;


import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class ProducterApp {
    public static void main(String[] args) throws JMSException {

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,

                ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");

        // JMS 客户端到JMS Provider 的连接

        Connection connection = connectionFactory.createConnection();

        connection.start();

        // Session： 一个发送或接收消息的线程

        Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);

        // Destination ：消息的目的地;消息发送给谁.

        // 获取session注意参数值my-queue是Query的名字

        Destination destination = session.createQueue("my-queue");

        // MessageProducer：消息生产者

        MessageProducer producer = session.createProducer(destination);

        // 设置不持久化

        producer.setDeliveryMode(DeliveryMode.PERSISTENT);

        // 发送一条消息

        for (int i = 1; i <= 5; i++) {

            sendMsg(session, producer, i);

        }

        session.commit();

        connection.close();
    }
    public static void sendMsg(Session session, MessageProducer producer, int i) throws JMSException {

        // 创建一条文本消息
        TextMessage message = session.createTextMessage("Hello ActiveMQ！" + i);

        // 通过消息生产者发出消息

        producer.send(message);

    }
}
